package com.xspace.android.xafcommon.view;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xspace.android.xafcommon.R;
import com.xspace.android.xafcommon.base.BaseFragment;
import com.xspace.android.xafcommon.base.PagerInfoView;

import cn.xspace.app.view.TitleBar;

/**
 * 通用的问题列表fragment
 */
public class CommonListFragment extends BaseFragment implements
        SuperRefreshLayout.SuperRefreshLayoutListener, AdapterView.OnItemClickListener {

    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_LOADING = 1;
    public static final int TYPE_NO_MORE = 2;
    public static final int TYPE_ERROR = 3;
    public static final int TYPE_NET_ERROR = 4;
    /**
     * 标题栏
     */
    protected TitleBar mTitleBar;
    /**
     * 列表
     */
    protected ListView mListView;
    protected SuperRefreshLayout mRefreshLayout;
    /**
     * 右下角操作按钮
     */
    protected FloatOperateButton operateButton;
//    protected RelativeLayout operateButton;
//    protected ImageView operateIcon;
//    protected TextView operateText;

    //    protected EmptyLayout mErrorLayout;
    protected boolean mIsRefresh;
    protected boolean mIsLoadMore;
    private ProgressBar mFooterProgressBar;
    private TextView mFooterText;
    //列表上方的容器,用于添加东西
    protected ViewGroup mBaseContainer;
    protected PagerInfoView mBasePagerInfo;

    @Override
    public View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_base_list, null);
    }

    @Override
    protected void initFindViewById(View root) {
        mTitleBar = (TitleBar) root.findViewById(R.id.base_list_title_bar);
        mListView = (ListView) root.findViewById(R.id.listView00);
        mRefreshLayout = (SuperRefreshLayout) root.findViewById(R.id.superRefreshLayout);
        mRefreshLayout.setColorSchemeResources(
                R.color.holo_blue_bright, R.color.holo_green_light,
                R.color.holo_orange_light, R.color.holo_red_light);
        mRefreshLayout.setSuperRefreshLayoutListener(this);
        mRefreshLayout.setVisibility(View.VISIBLE);
        operateButton = (FloatOperateButton) root.findViewById(R.id.operate_button);
        operateButton.setVisibility(View.GONE); //默认隐藏
//        operateButton = (RelativeLayout) root.findViewById(R.id.relative_operate);
//        operateIcon = (ImageView) root.findViewById(R.id.icon_float);
//        operateText = (TextView) root.findViewById(R.id.operate_text);
        View mFooterView = View.inflate(mContext, R.layout.layout_list_view_footer, null);
        mFooterText = (TextView) mFooterView.findViewById(R.id.tv_footer);
        mFooterProgressBar = (ProgressBar) mFooterView.findViewById(R.id.pb_footer);
        mBaseContainer = (FrameLayout) root.findViewById(R.id.base_list_container);
        mBasePagerInfo = (PagerInfoView) root.findViewById(R.id.base_list_pager_info);
        mListView.setOnItemClickListener(this);
        if (isNeedFooter()) {
            mListView.addFooterView(mFooterView);
        }
        if (isShowTop) {
            mTitleBar.setVisibility(View.VISIBLE);
        } else {
            mTitleBar.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initEvent() {
        if (isShowTop) {
            mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    exitActivity2();
                }
            });
        }
    }

    protected boolean isNeedFooter() {
        return true;
    }

    @Override
    public void initData() {

    }

    protected void onComplete() {
        mRefreshLayout.onLoadComplete();
        mIsRefresh = false;
        mIsLoadMore = false;
    }

    protected void setFooterType(int type) {
        try {
            switch (type) {
                case TYPE_NORMAL:
                case TYPE_LOADING:
                    mFooterText.setText(getResources().getString(R.string.footer_type_loading));
                    mFooterProgressBar.setVisibility(View.VISIBLE);
                    break;
                case TYPE_NET_ERROR:
                    mFooterText.setText(getResources().getString(R.string.footer_type_net_error));
                    mFooterProgressBar.setVisibility(View.GONE);
                    break;
                case TYPE_ERROR:
                    mFooterText.setText(getResources().getString(R.string.footer_type_error));
                    mFooterProgressBar.setVisibility(View.GONE);
                    break;
                case TYPE_NO_MORE:
                    mFooterText.setText(getResources().getString(R.string.footer_type_not_more));
                    mFooterProgressBar.setVisibility(View.GONE);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void requestData() {
    }

    @Override
    public void onRefreshing() {
        mRefreshLayout.setRefreshing(true);
        mIsRefresh = true;
        buildRefreshMap();
        requestData();
    }


    @Override
    public void onLoadMore() {
        setFooterType(TYPE_LOADING);
        mIsLoadMore = true;
        buildLoadMoreMap();
        requestData();
    }

    protected void buildRefreshMap() {

    }

    protected void buildLoadMoreMap() {

    }

    protected void itemClick(int position) {

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        itemClick(position);
    }

    public void removeMarginTop() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mRefreshLayout.getLayoutParams();
        layoutParams.setMargins(0, 0, 0, 0);
        mRefreshLayout.setLayoutParams(layoutParams);
    }

    public void removeDivider() {
        mListView.setDivider(getResources().getDrawable(R.color.color_divider));
        mListView.setDividerHeight(dip2px(0.5f));
    }

    private int dip2px(float dpValue) {
        float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
